package com.kinglone.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kinglone.model.RegionState;
import com.kinglone.service.RegionStateService;
import com.kinglone.util.JSONMessageView;


/**
 * 省市区县联动查询
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/regionState")
public class RegionStateController {

	@Resource
	private RegionStateService regionStateService;
	
	/**
	 * 查询省
	 * @param request
	 * @return
	 */
	@RequestMapping("/selProvince.jspx")
	public JSONMessageView selProvince(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1, "", null);
		try {
			List<RegionState> regionStateList = new ArrayList<RegionState>();
	        regionStateList = regionStateService.getRegionStateListByParentId(null);
	        json.setCode(0);
			json.setMessage("查询成功!");
	        json.setContent(regionStateList);
	        return json;
		} catch (Exception e) {
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("系统错误!");
			return json;
		}	
	} 
	/**
	 * 根据省份id查询市
	 * @param request
	 * @return
	 */
	@RequestMapping("/selCityByprovinceID.jspx")
	public JSONMessageView selCityByprovinceID(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1, "", null);
		try {
			String provinceID = request.getParameter("provinceID");
			List<RegionState> cityList = new ArrayList<RegionState>();
			cityList = regionStateService.getCityListByProvinceID(provinceID);
			json.setCode(0);
			json.setMessage("查询成功!");
			json.setContent(cityList);
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("系统错误!");
			return json;
		}	
	} 
	/**
	 * 根据市区id查询县
	 * @param request
	 * @return
	 */
	@RequestMapping("/selAreaBycityID.jspx")
	public JSONMessageView selAreaBycityID(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1, "", null);
		try {
			String cityID = request.getParameter("cityID");
			List<RegionState> areaList = new ArrayList<RegionState>();
			areaList = regionStateService.getAreaListByCityID(cityID);
			json.setCode(0);
			json.setMessage("查询成功!");
			json.setContent(areaList);
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("系统错误!");
			return json;
		}	
	} 
	/**
	 * 根据县查询地址
	 * @param request
	 * @return
	 */
	@RequestMapping("/selAddrByAreaID.jspx")
	public JSONMessageView selAddrByAreaID(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1, "", null);
		try {
			String areaID = request.getParameter("areaID");
			String addrString = regionStateService.selAddrByAreaID(areaID);
			System.out.println(addrString);
			json.setCode(0);
			json.setContent(addrString);
			json.setMessage("查询成功!");
			System.out.println(json);
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("系统错误!");
			return json;
		}
	}
}
